package me.zhengjie.minbearwcs.serivce.mission.generate.outbound;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.minbearwcs.dao.mission.inf.WcsMissionDao;
import me.zhengjie.minbearwcs.dao.outbound.inf.OutboundTaskRecordDao;
import me.zhengjie.minbearwcs.dao.stock.inf.PalletDao;
import me.zhengjie.minbearwcs.dao.storage.inf.SmallStorageDao;
import me.zhengjie.minbearwcs.dao.storage.inf.StorageDao;
import me.zhengjie.minbearwcs.entity.DTO.transfer.Tuple;
import me.zhengjie.minbearwcs.entity.PO.wcs.mission.WcsMission;
import me.zhengjie.minbearwcs.entity.PO.wcs.mission.status.OutboundAfterSortMissionStatus;
import me.zhengjie.minbearwcs.entity.PO.wcs.mission.status.OutboundSortMissionStatus;
import me.zhengjie.minbearwcs.entity.PO.wcs.mission.type.MissionType;
import me.zhengjie.minbearwcs.entity.PO.wcs.outbound.common.OutboundContentPool;
import me.zhengjie.minbearwcs.entity.PO.wcs.outbound.record.OutboundTaskRecord;
import me.zhengjie.minbearwcs.entity.PO.wcs.stock.Pallet;
import me.zhengjie.minbearwcs.entity.PO.wcs.storage.SmallStorage;
import me.zhengjie.minbearwcs.entity.PO.wcs.storage.type.StorageType;
import me.zhengjie.minbearwcs.serivce.mission.generate.MissionGenerator;
import me.zhengjie.minbearwcs.serivce.mission.generate.type.MissionGenType;
import me.zhengjie.minbearwcs.serivce.mission.storageProcessor.locker.Floor5Or6.OutboundStorage5Or6FloorLocker;
import me.zhengjie.minbearwcs.utils.IdWorker;
import org.aspectj.weaver.patterns.IfPointcut;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Slf4j
@Component
@RequiredArgsConstructor
public class OutboundSortAfterMissionGenerator implements MissionGenerator<String> {

    private final OutboundTaskRecordDao outboundTaskRecordDao;
    private final WcsMissionDao wcsMissionDao;
    private final IdWorker idWorker;
    private final SmallStorageDao smallStorageDao;
    private final StorageDao storageDao;
    private final PalletDao palletDao;
    private final OutboundStorage5Or6FloorLocker outboundStorage5Or6FloorLocker;


    @Override
    public MissionGenType type() {
        return MissionGenType.OUTBOUND_SORT_AFTER;
    }

    @Override
    @Transactional
    public Tuple<String> generate(Object source) {
        String startQrPoint = (String) source;

        SmallStorage smallStorage = smallStorageDao.selectByQrCode(startQrPoint);

        if (smallStorage == null){
            return Tuple.tuple("该点位不能叫车",false);
        }

        StorageType storageType = storageDao.selectStorageTypeBySmallStorage(smallStorage.getSmallStorageCode());
        if (StorageType.AFTER_SORT_AREA_5F != storageType && StorageType.AFTER_SORT_AREA_6F != storageType){
            return Tuple.tuple("该点位不能叫车",false);
        }

        // 判断有无重复叫车
        if (smallStorage.getOutboundFreezePallet() != null) {
            return Tuple.tuple("已叫车，请勿重复操作", false);
        }

        Pallet pallet = palletDao.selectByPalletCode(smallStorage.getPalletCode());

        if (pallet == null){
            return Tuple.tuple("该地标无栈板，请确认", false);
        }


        // 生成有起点的任务
        OutboundTaskRecord outboundTaskRecord = new OutboundTaskRecord(null, "outbound_record_" + idWorker.nextId(), null,
                null, smallStorage.getPalletCode(), null,
                null, null, smallStorage.getSmallStorageCode(), null, null,
                OutboundContentPool.OUTBOUND_RECORD_SORT, null, LocalDateTime.now(), null, pallet.getLogisticsCompanyCode());

        String missionId = "Task_Outbound_Sort_After_" + idWorker.nextId();

        WcsMission wcsMission = new WcsMission(null, missionId, null, MissionType.OUTBOUND_AFTER_SORT.name(),
                OutboundAfterSortMissionStatus.WAIT_FOR_STORAGE.name(), LocalDateTime.now(), null, null,
                1, outboundTaskRecord.getOutboundRecordId());
        wcsMissionDao.insert(wcsMission);
        outboundTaskRecordDao.insert(outboundTaskRecord);

        outboundStorage5Or6FloorLocker.lockStorage(smallStorage.getSmallStorageCode(), smallStorage.getPalletCode());

        return Tuple.tuple(null, true);
    }
}
